package com.example.smartclassroom.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.smartclassroom.entity.Course;
import com.example.smartclassroom.entity.CourseSelection;
import com.example.smartclassroom.entity.request.CourseRequest;
import com.example.smartclassroom.entity.vo.CourseVo;
import com.example.smartclassroom.enums.BorrowStatusEnum;
import com.example.smartclassroom.enums.UserType;
import com.example.smartclassroom.mapper.CourseMapper;
import com.example.smartclassroom.mapper.CourseSelectionMapper;
import com.example.smartclassroom.service.CourseSelectionService;
import com.example.smartclassroom.util.JsonDate;
import com.example.smartclassroom.util.LoginUserInfoUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;
import java.util.Optional;

@Service
@Transactional
public class CourseSelectionSeiveceImpl extends ServiceImpl<CourseSelectionMapper, CourseSelection> implements CourseSelectionService{
    @Autowired
    private CourseSelectionMapper courseSelectionMapper;

    @Autowired
    private CourseMapper courseMapper;
    /**
     * 学生查询课程信息
     */
    public JsonDate queryAllCourseByStudent(CourseRequest courseRequest){
        JsonDate date = new JsonDate();
        Page<CourseVo> page =new Page<>(
                courseRequest.getOffset()-1, courseRequest.getPageSize());
        Page<CourseVo> courseVos = courseSelectionMapper.selectAll(page,courseRequest);
        date.setData(courseVos);
        return date;
    }


    /**
     * 查询学生选课信息、老师自己的课程
     */
    public JsonDate queryCourseByUserId(CourseRequest courseRequest){
        JsonDate date = new JsonDate();
        int loginUserId  =LoginUserInfoUtils.getLoginUserId();
        UserType loginUserType = LoginUserInfoUtils.getLoginUserType();
        if (UserType.TEACHER.equals(loginUserType)){
            courseRequest.setTeacherId(loginUserId);
        }else if (UserType.STUDENT.equals(loginUserType)){
            courseRequest.setStudentId(loginUserId);
        }
        Page<CourseVo> page =new Page<>(
                Optional.ofNullable(courseRequest.getOffset()).orElse(1) - 1,
                Optional.ofNullable(courseRequest.getPageSize()).orElse(1));
        Page<CourseVo> courseVos = courseSelectionMapper.selectAll(page,courseRequest);
        date.setData(courseVos);
        return  date;
    }

    /**
     * 查询学生选课信息、老师自己的课程,一以便导出
     */
    public List<CourseSelection> queryCourseByUserIdOut(Integer studentId){
        JsonDate date = new JsonDate();
         studentId=LoginUserInfoUtils.getLoginUserId();
        return  courseSelectionMapper.selectAllOut(studentId);
    }


    /**
     * 学生新增课程判断人数是否已满，人数已满添加失败；
     * 学生判断上课课程时间是否冲突
     * 学生判断是否在选课时间范围内
     */
    public JsonDate studentAddCourse(CourseSelection courseSelection){
        JsonDate date = new JsonDate();
        //获取该课程容纳人数
        Integer num=courseSelection.getNum();
        //获取现有人数
        Integer joinNum= courseSelection.getJoinNum();
        //获取课程开始时间
        LocalDate startTime=courseSelection.getStartTime();
        //获取课程结束时间
        LocalDate endTime=courseSelection.getEndTime();
        //获取星期
        String weekDay=courseSelection.getWeekDay();
        //获取时间段
        String segment=courseSelection.getSegment();
        //获取选课开始时间
        LocalDate startTimeChoose=courseSelection.getStartTimeChoose();
        //获取选课结束时间
        LocalDate endTimeChoose=courseSelection.getEndTimeChoose();
        //获取教室id
        Integer roomId=courseSelection.getRoomId();
        //获取用户id
        Integer userId= LoginUserInfoUtils.getLoginUserId();
        courseSelection.setStudentId(userId);
        //获取课程id
        Integer courseId=courseSelection.getCourseId();
        System.out.println(courseId);
        //获取当前日期
        LocalDate nowTime=LocalDate.now();
        //判断当前时间是否在选课时间范围内
        int row=0;
        if(nowTime.isAfter(startTimeChoose)&&nowTime.isBefore(endTimeChoose)){
            //判断选课人员是否已满
            if(joinNum<=num){
                //判断课程时间是否冲突
                //查询当前用户已有课程
                List<CourseSelection> courseSelections=courseSelectionMapper.selectCourseByStudentId(userId);
                for (CourseSelection courseSelection1:courseSelections){
                    //查询当前课程信息
                    Course tmpCourse=courseMapper.selectCourseById(courseSelection1.getId());
                    if (tmpCourse.getWeekDay().equals(weekDay)&&tmpCourse.getSegment().equals(segment)&&tmpCourse.getRoomId().equals(roomId)){
                        row =0;
                        return JsonDate.errorEnum(BorrowStatusEnum.COURSE_TIME_ERROR);
                    }else {
                        row=1;
                    }
                }
            }else {
                return JsonDate.errorEnum(BorrowStatusEnum.COURSE_NUM_ERROR);
            }
        }else {
            return JsonDate.errorEnum(BorrowStatusEnum.COURSE_CHOOSE_OVER_TIME_ERROR);
        }
        //成功后添加
        if (row==1){
            date.setData(courseSelectionMapper.studentAddCourse(userId,courseId));
        }
        return date;
    }


    /**
     * 查询学生已选课或老师本人的课程总数
     * @param courseSelection
     * @return
     */
    public Integer queryTotalByStudentId(CourseSelection courseSelection){
        Integer userId= LoginUserInfoUtils.getLoginUserId();
        courseSelection.setStudentId(userId);
        return courseSelectionMapper.queryTotalByStudentId(courseSelection.getStudentId());
    }

    /**
     * 通过学生id和课程id删除课程
     */
    public JsonDate deleteCourseByStudentIdAndCourseId(CourseSelection courseSelection){
        JsonDate date = new JsonDate();
        //获取用户id
        Integer userId= LoginUserInfoUtils.getLoginUserId();
        //获取课程id
        Integer courseId=courseSelection.getCourseId();
        //获取选课开始时间
        LocalDate startTime=courseSelection.getStartTimeChoose();
        //获取选课结束时间
        LocalDate endTime=courseSelection.getEndTimeChoose();
        //获取当前时间
        LocalDate nowTime=LocalDate.now();
        //判断当前时间是否在选课时间范围内
        if (nowTime.isAfter(startTime)&&nowTime.isBefore(endTime)){
            date.setData(courseSelectionMapper.deleteCourseByStudentIdAndCourseId(userId,courseId));
            return date;
        }else {
            return JsonDate.errorEnum(BorrowStatusEnum.COURSE_OVER_TIME_ERROR);
        }
    }


}
